package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.controller.BaseController;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.domain.dto.DataboardCatalogDTO;
import com.jcfk.system.domain.query.DataboardCatalogQuery;
import com.jcfk.system.service.DataboardCatalogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据看板目录维护 服务接口
 *
 * @author 李炳海
 */
@Api(value = "/databoardCatalog", tags = {"数据看板目录维护 服务接口"})
@RestController
@RequestMapping("/databoardCatalog")
public class DataboardCatalogController extends BaseController {
    /**
     * 数据看板目录服务
     */
    @Autowired
    private DataboardCatalogService databoardCatalogService;

    /**
     * 查询目录列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "查询目录列表", notes = "查询目录列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<DataboardCatalogDTO>> getList(@RequestBody DataboardCatalogQuery query) {
        List<DataboardCatalogDTO> list = databoardCatalogService.getList(query);
        return RetResult.ok(list);
    }

    /**
     * 根据id获取目录信息
     *
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "根据id获取目录信息", notes = "根据id获取目录信息", httpMethod = "POST")
    @PostMapping("get/{id}")
    public RetResult<DataboardCatalogDTO> get(@PathVariable(name = "id") String id) {
        if (StrUtil.isEmpty(id)) {
            throw new RuntimeException("主键不能为空");
        }
        return RetResult.ok(databoardCatalogService.get(id));
    }

    /**
     * 保存目录
     *
     * @param multiLevelTree
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DataboardCatalogDTO", name = "multiLevelTree", value = "", required = true)
    })
    @ApiOperation(value = "保存目录", notes = "保存目录", httpMethod = "POST")
    @RequestLogHeader(modular = "数据看板目录", businessType = "保存")
    @PostMapping("/save")
    public RetResult<DataboardCatalogDTO> save(@RequestBody DataboardCatalogDTO multiLevelTree) {
        // 数据校验
        RetResult ret = databoardCatalogService.isValid(multiLevelTree);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(multiLevelTree.getId())) {
            // 新增
            return RetResult.ok(databoardCatalogService.insert(multiLevelTree));
        } else {
            // 更新
            return RetResult.ok(databoardCatalogService.update(multiLevelTree));
        }
    }

    /**
     * 删除列表(逻辑删除)
     *
     * @param dto
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DataboardCatalogDTO", name = "dto", value = "", required = true)
    })
    @ApiOperation(value = "删除列表(逻辑删除)", notes = "删除列表(逻辑删除)", httpMethod = "POST")
    @RequestLogHeader(modular = "数据看板目录", businessType = "删除列表")
    @PostMapping("delete")
    public RetResult<?> delete(@RequestBody DataboardCatalogDTO dto) {

        // 校验
        for (String id : dto.getIds()) {
            Boolean haveChild = databoardCatalogService.isHaveChild(id);
            if (haveChild.equals(true)) {
                return RetResult.fail("不能删除有下级机构的目录");
            }
        }
        databoardCatalogService.logicDeleteList(dto.getIds());
        return RetResult.ok();
    }

    /**
     * 批量物理删除(通过ids)
     *
     * @param ids
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "String[]", name = "ids", value = "", required = true)
    })
    @ApiOperation(value = "批量物理删除(通过ids)", notes = "批量物理删除(通过ids)", httpMethod = "POST")
    @RequestLogHeader(modular = "数据看板目录", businessType = "删除列表")
    @PostMapping("deleteByIds")
    public RetResult<?> deleteByIds(@RequestBody String[] ids) {
        return RetResult.ok(databoardCatalogService.deleteByIds(ids));
    }

    /**
     * 获取目录
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DataboardCatalogQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取目录", notes = "获取目录", httpMethod = "POST")
    @PostMapping("getTree")
    public RetResult<List<DataboardCatalogDTO>> getTree(@RequestBody DataboardCatalogQuery query) {
        return RetResult.ok(databoardCatalogService.getTree(query));
    }

    /**
     * 获取可用目录
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DataboardCatalogQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取可用目录", notes = "获取可用目录", httpMethod = "POST")
    @PostMapping("getUsableTree")
    public RetResult<List<DataboardCatalogDTO>> getUsableMultiLevelTree(@RequestBody DataboardCatalogQuery query) {
         return RetResult.ok(databoardCatalogService.getTree(query));
    }

    /**
     * 判断是否有下级目录
     *
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "判断是否有下级目录", notes = "判断是否有下级目录", httpMethod = "POST")
    @PostMapping("isHaveChild")
    public RetResult<?> isHaveChild(@RequestParam String id) {
        return RetResult.ok(databoardCatalogService.isHaveChild(id));
    }
}
